/*
 * @Description: 
 * @Author: 47
 * @Date: 2022-12-31 17:24:45
 * @LastEditors: 47
 * @LastEditTime: 2023-01-11 19:47:35
 */
// 引入连接池
const db = require('../db')

const result = (req, res) => {

    //三种参数
    let phone = req.params.phone
    let username = req.body.username
    let token = req.headers.token

    user = {
        id: results[0].user_id,
        username: results[0].user_name,
        phone: results[0].user_phone,
    }

    let sql = 'SELECT * FROM users where ?,?,?'
    let data = [phone,username]
    db.query(sql, data, (err, results) => {
        
        //err出错
        if (err) {
            return res.status(500).json({
                code: 500,
                msg: '服务器出错误，请联系管理员',
                data: null
            })
        }

        //成功
        res.status(200).json({
            code: 200,
            msg: '添加成功',
            data: null
        })

        //失败
        res.status(200).json({
            code: 400,
            msg: '验证码错误',
            data: null
        })

    })
}


const select = (req,res) => {

    let userId = req.params.userId;
    let sql = `SELECT * FROM order_recipients WHERE recipients_user_id = ?`
    let data = [userId]
    

    db.query(sql, data, (err, result) => {
        //err出错
        if (err) {
            return res.status(500).json({
                code: 500,
                msg: '服务器出错误，请联系管理员',
                data: null
            })
        }

        //成功
        res.status(200).json({
            code: 200,
            msg: '添加成功',
            data: null
        })


        
    })
    
}



const add = (req,res) => {
    //添加
    // 订单号
    let orderId = req.body.orderId;
    // 用户id
    let orderUserId = req.body.orderUserId;

    let sql = `INSERT INTO orders(order_id,order_user_id) VALUES(?,?)`
    let data = [orderId, orderUserId]

    db.query(sql, data, (err, result) => {
    })
}

const deleteAndupdate = (req,res) => {
    // 删除
    let sql = `DELETE FROM shopitems WHERE item_id = ?`;
    // 获取当前修改商品的id
    let data = [result[0].item_id];

    //更新
    sql = 'update goods set good_num = good_num -1  where good_id = ?;';


}

module.exports = {
    result,
    select,
    add,
    deleteAndupdate
}